package org.jeecg.modules.demo.sysCutting.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecg.modules.demo.sysCutting.entity.SysCutting;
import org.jeecg.modules.demo.sysCutting.entity.SysCuttingStorage;
import org.jeecg.modules.demo.sysCutting.entity.SysCuttingSpecific;
import org.jeecg.modules.demo.sysCutting.mapper.SysCuttingStorageMapper;
import org.jeecg.modules.demo.sysCutting.mapper.SysCuttingSpecificMapper;
import org.jeecg.modules.demo.sysCutting.mapper.SysCuttingMapper;
import org.jeecg.modules.demo.sysCutting.service.ISysCuttingService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 刀具信息
 * @Author: jeecg-boot
 * @Date:   2022-10-28
 * @Version: V1.0
 */
@Service
public class SysCuttingServiceImpl extends ServiceImpl<SysCuttingMapper, SysCutting> implements ISysCuttingService {

	@Autowired
	private SysCuttingMapper sysCuttingMapper;
	@Autowired
	private SysCuttingStorageMapper sysCuttingStorageMapper;
	@Autowired
	private SysCuttingSpecificMapper sysCuttingSpecificMapper;




	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(SysCutting sysCutting, List<SysCuttingStorage> sysCuttingStorageList,List<SysCuttingSpecific> sysCuttingSpecificList) {
		sysCutting.setCuttingToolid("1");

		sysCuttingMapper.insert(sysCutting);
		if(sysCuttingStorageList!=null && sysCuttingStorageList.size()>0) {
			for(SysCuttingStorage entity:sysCuttingStorageList) {
				//外键设置
				entity.setToolCode(sysCutting.getCuttingToolid());
				sysCuttingStorageMapper.insert(entity);
			}
		}
		if(sysCuttingSpecificList!=null && sysCuttingSpecificList.size()>0) {
			for(SysCuttingSpecific entity:sysCuttingSpecificList) {
				//外键设置
				entity.setToolCode(sysCutting.getCuttingToolid());
				sysCuttingSpecificMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(SysCutting sysCutting,List<SysCuttingStorage> sysCuttingStorageList,List<SysCuttingSpecific> sysCuttingSpecificList) {
		sysCuttingMapper.updateById(sysCutting);
		
		//1.先删除子表数据
		sysCuttingStorageMapper.deleteByMainId(sysCutting.getId());
		sysCuttingSpecificMapper.deleteByMainId(sysCutting.getId());
		
		//2.子表数据重新插入
		if(sysCuttingStorageList!=null && sysCuttingStorageList.size()>0) {
			for(SysCuttingStorage entity:sysCuttingStorageList) {
				//外键设置
				entity.setToolCode(sysCutting.getCuttingToolid());
				sysCuttingStorageMapper.insert(entity);
			}
		}
		if(sysCuttingSpecificList!=null && sysCuttingSpecificList.size()>0) {
			for(SysCuttingSpecific entity:sysCuttingSpecificList) {
				//外键设置
				entity.setToolCode(sysCutting.getCuttingToolid());
				sysCuttingSpecificMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		sysCuttingStorageMapper.deleteByMainId(id);
		sysCuttingSpecificMapper.deleteByMainId(id);
		sysCuttingMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			sysCuttingStorageMapper.deleteByMainId(id.toString());
			sysCuttingSpecificMapper.deleteByMainId(id.toString());
			sysCuttingMapper.deleteById(id);
		}
	}
	
}
